package STUDENT.TRINHQUANGXUANDUC.ADVANCED.SWING;

import java.awt.EventQueue;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

public class Views extends JFrame {

	private JPanel contentPane;
	private JTextField txtA;
	private JTextField txtB;
	private JTextField txtresult;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Views frame = new Views();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public Views() {
		setTitle("Tinh toan");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 240, 174);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		JLabel lblSoA = new JLabel("So A:");
		lblSoA.setBounds(42, 11, 46, 14);
		contentPane.add(lblSoA);

		JLabel lblSoB = new JLabel("So B:");
		lblSoB.setBounds(42, 36, 46, 14);
		contentPane.add(lblSoB);

		JLabel lblKetQua = new JLabel("Ket qua:");
		lblKetQua.setBounds(42, 70, 46, 14);
		contentPane.add(lblKetQua);

		txtA = new JTextField();
		txtA.setBounds(92, 8, 86, 20);
		contentPane.add(txtA);
		txtA.setColumns(10);

		txtB = new JTextField();
		txtB.setBounds(92, 36, 86, 20);
		contentPane.add(txtB);
		txtB.setColumns(10);

		txtresult = new JTextField();
		txtresult.setBounds(92, 67, 86, 20);
		contentPane.add(txtresult);
		txtresult.setColumns(10);

		JButton btnTinh = new JButton("Tinh");
		btnTinh.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				try {
					if (Integer.parseInt(txtA.getText()) != 0 && Integer.parseInt(txtB.getText()) == 0) {
						JOptionPane.showMessageDialog(null, "Khong the chia cho khong");
						txtB.requestFocus();
					} else if (Integer.parseInt(txtA.getText()) == 0 && Integer.parseInt(txtB.getText()) == 0) {
						JOptionPane.showMessageDialog(null, "Khong the tinh toan");
					} else {
						String kq = Integer.parseInt(txtA.getText()) / Integer.parseInt(txtB.getText()) + "";
						txtresult.setText(kq.trim());
					}
				} catch (NumberFormatException ex) {
					// TODO: handle exception
					JOptionPane.showMessageDialog(null, ex.getMessage());
				} catch (Exception e) {
					JOptionPane.showMessageDialog(null, e.getMessage());
				}
			}
		});
		btnTinh.setBounds(89, 101, 89, 23);
		contentPane.add(btnTinh);
	}
}
